package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnHouseEvaluateBean;
import com.example.zhunongzonghe.dao.ZnHouseEvaluateMapper;
import com.example.zhunongzonghe.entity.*;
import com.example.zhunongzonghe.service.IZnFarmhouseReserveService;
import com.example.zhunongzonghe.service.IZnUsersService;
import com.example.zhunongzonghe.service.ZnHouseEvaluateService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 蔡某人
* @description 针对表【zn_house_evaluate】的数据库操作Service实现
* @createDate 2024-04-23 20:31:53
*/
@Service
public class ZnHouseEvaluateServiceImpl extends ServiceImpl<ZnHouseEvaluateMapper, ZnHouseEvaluate>
    implements ZnHouseEvaluateService{
    @Resource
    private ZnHouseEvaluateMapper znHouseEvaluateMapper;
    @Resource
    private IZnUsersService usersService;
    @Resource
    private IZnFarmhouseReserveService houseReserveService;




    @Override
    public void addHouseEvaluate(ZnHouseEvaluateBean znHouseEvaluateBean, String userName) {
        ZnHouseEvaluate znHouseEvaluate = BeanCopyUtils.copyBean(znHouseEvaluateBean,ZnHouseEvaluate.class);
        znHouseEvaluate.setUsername(userName);
        znHouseEvaluate.setCreateTime(LocalDateTime.now());
        znHouseEvaluate.setUpdateTime(LocalDateTime.now());
        znHouseEvaluateMapper.insert(znHouseEvaluate);
        LambdaQueryWrapper<ZnFarmhouseReserve> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnFarmhouseReserve::getReserveId,znHouseEvaluate.getReserveId());
        ZnFarmhouseReserve reserve = houseReserveService.getOne(queryWrapper);
        reserve.setEvaluateStatus(1);
        houseReserveService.updateById(reserve);
    }

    @Override
    public void editHouseEvaluate(ZnHouseEvaluateBean znHouseEvaluateBean, String userName) {
        ZnHouseEvaluate znHouseEvaluate = BeanCopyUtils.copyBean(znHouseEvaluateBean,ZnHouseEvaluate.class);
        znHouseEvaluate.setUsername(userName);
        znHouseEvaluate.setUpdateTime(LocalDateTime.now());
        znHouseEvaluateMapper.updateById(znHouseEvaluate);
    }

    @Override
    public ZnHouseEvaluateBean getEvaluate(Integer reserveId,String username) {
        LambdaQueryWrapper<ZnHouseEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnHouseEvaluate::getReserveId,reserveId).eq(ZnHouseEvaluate::getUsername,username);
        ZnHouseEvaluate znHouseEvaluate = znHouseEvaluateMapper.selectOne(queryWrapper);
        if (znHouseEvaluate==null) return null;
        ZnHouseEvaluateBean bean = BeanCopyUtils.copyBean(znHouseEvaluate,ZnHouseEvaluateBean.class);
        return bean;
    }

    @Override
    public Page<ZnHouseEvaluateBean> selectEvaAll(Integer houseId, Integer createPage, Integer pageSize) {
        Page<ZnHouseEvaluate> page = new Page<>(createPage,pageSize);
        LambdaQueryWrapper<ZnHouseEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnHouseEvaluate::getHouseId,houseId);
        Page<ZnHouseEvaluate> houseEvaluatePage = znHouseEvaluateMapper.selectPage(page,queryWrapper);
        Page<ZnHouseEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(houseEvaluatePage,ZnHouseEvaluateBean.class);
        Integer averageScore = averageScore(houseId);
        Long sum = EvaluateSum(houseId);
        beanPage.getRecords().forEach(evaluateBean -> {
            ZnUsers znUsers = usersService.getUserName(evaluateBean.getUsername());
            evaluateBean.setAvatar(znUsers.getAvatar());
            evaluateBean.setNickname(znUsers.getNickname());
            ZnFarmhouseReserve houseReserve =
                    houseReserveService.getById(evaluateBean.getReserveId());
            evaluateBean.setPlayDay(houseReserve.getPlayDays());
            evaluateBean.setAverageMarks(averageScore);
            evaluateBean.setEvaluateSum(sum);
        });
        return beanPage;
    }

    @Override
    public Page<ZnHouseEvaluateBean> selectFourAndFive(Integer houseId, Integer createPage, Integer pageSize) {
        Page<ZnHouseEvaluate> page = new Page<>(createPage,pageSize);
        LambdaQueryWrapper<ZnHouseEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnHouseEvaluate::getHouseId,houseId).eq(ZnHouseEvaluate::getScore, 5)
                .or(qw -> qw.eq(ZnHouseEvaluate::getHouseId,houseId).eq(ZnHouseEvaluate::getScore, 4));
        Page<ZnHouseEvaluate> houseEvaluatePage = znHouseEvaluateMapper.selectPage(page,queryWrapper);
        Long sum = EvaluateSum(houseId);
        int averageScore = averageScore(houseId);
        Page<ZnHouseEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(houseEvaluatePage,ZnHouseEvaluateBean.class);
        beanPage.getRecords().forEach(evaluateBean -> {
            ZnUsers znUsers = usersService.getUserName(evaluateBean.getUsername());
            evaluateBean.setAvatar(znUsers.getAvatar());
            evaluateBean.setNickname(znUsers.getNickname());
            ZnFarmhouseReserve houseReserve =
                    houseReserveService.getById(evaluateBean.getReserveId());
            evaluateBean.setPlayDay(houseReserve.getPlayDays());
            evaluateBean.setAverageMarks(averageScore);
            evaluateBean.setEvaluateSum(sum);
        });
        return beanPage;
    }

    @Override
    public Page<ZnHouseEvaluateBean> selectThree(Integer houseId, Integer createPage, Integer pageSize) {
        Page<ZnHouseEvaluate> page = new Page<>(createPage,pageSize);
        LambdaQueryWrapper<ZnHouseEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnHouseEvaluate::getHouseId,houseId).eq(ZnHouseEvaluate::getScore, 3);
        Page<ZnHouseEvaluate> houseEvaluatePage = znHouseEvaluateMapper.selectPage(page,queryWrapper);
        Long sum = EvaluateSum(houseId);
        int averageScore = averageScore(houseId);
        Page<ZnHouseEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(houseEvaluatePage,ZnHouseEvaluateBean.class);
        beanPage.getRecords().forEach(evaluateBean -> {
            ZnUsers znUsers = usersService.getUserName(evaluateBean.getUsername());
            evaluateBean.setAvatar(znUsers.getAvatar());
            evaluateBean.setNickname(znUsers.getNickname());
            ZnFarmhouseReserve houseReserve =
                    houseReserveService.getById(evaluateBean.getReserveId());
            evaluateBean.setPlayDay(houseReserve.getPlayDays());
            evaluateBean.setAverageMarks(averageScore);
            evaluateBean.setEvaluateSum(sum);
        });
        return beanPage;
    }

    @Override
    public Page<ZnHouseEvaluateBean> selectOneAndTow(Integer houseId, Integer createPage, Integer pageSize) {
        Page<ZnHouseEvaluate> page = new Page<>(createPage,pageSize);
        LambdaQueryWrapper<ZnHouseEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnHouseEvaluate::getHouseId,houseId).eq(ZnHouseEvaluate::getScore, 1)
                .or(qw -> qw.eq(ZnHouseEvaluate::getHouseId,houseId).eq(ZnHouseEvaluate::getScore, 2));
        Page<ZnHouseEvaluate> houseEvaluatePage = znHouseEvaluateMapper.selectPage(page,queryWrapper);
        Long sum = EvaluateSum(houseId);
        int averageScore = averageScore(houseId);
        Page<ZnHouseEvaluateBean> beanPage = BeanCopyUtils.copyBeanPage(houseEvaluatePage,ZnHouseEvaluateBean.class);
        beanPage.getRecords().forEach(evaluateBean -> {
            ZnUsers znUsers = usersService.getUserName(evaluateBean.getUsername());
            evaluateBean.setAvatar(znUsers.getAvatar());
            evaluateBean.setNickname(znUsers.getNickname());
            ZnFarmhouseReserve houseReserve =
                    houseReserveService.getById(evaluateBean.getReserveId());
            evaluateBean.setPlayDay(houseReserve.getPlayDays());
            evaluateBean.setAverageMarks(averageScore);
            evaluateBean.setEvaluateSum(sum);
        });
        return beanPage;
    }

    @Override
    public Page<ZnHouseEvaluateBean> selectEvaluateAll(Integer currentPage, Integer pageSize, Object searchInfo) {
        Page<ZnHouseEvaluateBean> page = new Page<>(currentPage,pageSize);
        MPJLambdaWrapper<ZnHouseEvaluate> mpjLambdaWrapper = new MPJLambdaWrapper<ZnHouseEvaluate>()
                .selectAll(ZnHouseEvaluate.class)
                .select(ZnUsers::getNickname)
                .select(ZnFarmhouse::getHouseName)
                .like(ZnFarmEvaluate::getContent,searchInfo)
                .or().like(ZnUsers::getNickname,searchInfo)
                .or().like(ZnFarmhouse::getHouseName,searchInfo)
                .innerJoin(ZnUsers.class,ZnUsers::getUserName,ZnHouseEvaluate::getUsername)
                .innerJoin(ZnFarmhouse.class,ZnFarmhouse::getHouseId,ZnHouseEvaluate::getHouseId);
        Page<ZnHouseEvaluateBean> beanPage = znHouseEvaluateMapper.selectJoinPage(page,ZnHouseEvaluateBean.class,mpjLambdaWrapper);
        return beanPage;
    }

    private int averageScore(Integer houseId){
        LambdaQueryWrapper<ZnHouseEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnHouseEvaluate::getHouseId,houseId);
        List<ZnHouseEvaluate> evaluateList = znHouseEvaluateMapper.selectList(queryWrapper);
        int averageScore = (int) evaluateList.stream()
                .mapToInt(ZnHouseEvaluate::getScore)
                .average()
                .orElse(0.0);
        return averageScore;
    }
    private Long EvaluateSum(Integer houseId){
        LambdaQueryWrapper<ZnHouseEvaluate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnHouseEvaluate::getHouseId,houseId);
        Long sum = znHouseEvaluateMapper.selectCount(queryWrapper);
        return sum;
    }
}




